package com.guodexian.service.impl;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.guodexian.service.SmsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import static com.aliyun.teautil.Common.toJSONString;
/**
 * 类说明：<br>
 *
 * @author qfrx<br>
 * 2024/12/28 16:48<br>
 * @since 1.0 <br>
 */
@Service
public class AliyunSmsServiceImpl implements SmsService {
    @Value("${aliyun.ak}") String ak;
    @Value("${aliyun.sk}")String sk;
    public  Client createClient() throws Exception {
        Config config = new Config()
                // 配置 AccessKey ID，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId(ak)
                // 配置 AccessKey Secret，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret(sk);

        // 配置 Endpoint
        config.endpoint = "dysmsapi.aliyuncs.com";

        return new Client(config);
    }
    @Override
    public String sendSms(String mobile, String code) throws Exception {
        // 初始化请求客户端
        Client client = createClient();
        System.out.println(code);
        // 构造请求对象，请填入请求参数值
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(mobile)
                .setSignName("阿里云短信测试")
                .setTemplateCode("SMS_154950909")
                .setTemplateParam("{\"code\":\"" + code + "\"}");

        // 获取响应对象
        SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);

        // 响应包含服务端响应的 body 和 headers
        return toJSONString(sendSmsResponse);
    }
}
